<?php
declare (strict_types = 1);

namespace app\superadmin\controller;

use think\facade\Db;
use think\Request;

class pubfunc
{
    //新增
    public function save(Request $request)
    {
        $data = $request -> all();

        $data['updated_at'] = time();

        $res = Db::name($data['name']) ->strict(false) -> insert($data);

        if($res){
            return 'success';
        }
    }

    //更新
    public function update(Request $request)
    {
        $data = $request -> all();
        if(!empty($data['password'])){
            if($data['password'] == $data['repass']){
                unset($data['repass']);
                $data['password'] = md5($data['password']);
            }
        }else{
            unset($data['password']);
        }

        $res = Db::name($data['name']) ->strict(false) -> update($data);
        if($res){
        }
        return '修改成功';
    }

    //修改状态
    public function changeStatus(Request $request){
        $data = $request -> all();
        $res = Db::name($data['name'])->strict(false)->update($data);
        if($res){
            return json(['code'=>1,'msg'=>'修改成功']);
        }
    }

    //单条删除
    public function delete(Request $request)
    {
        $data = $request -> all();
        $res = Db::name($data['name'])->strict(false)->delete($data);
        if($res){
            return json(['code'=>1,'msg'=>'删除成功']);
        }
    }

    //批量删除
    public function deleteAll(Request $request)
    {
        $data = $request -> all();
        $res = Db::name($data['name'])->delete($data['id']);
        if($res){
            return json(['code'=>1,'msg'=>'删除成功']);
        }
    }

}
